package com.hyjr.collect.netty.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hyjr.bean.TaskBean;
import com.hyjr.collect.bcprov.SM2Utils;
import com.hyjr.collect.common.Constants;
import com.hyjr.collect.netty.handler.SendTaskClientHandler;
import com.hyjr.common.enums.CheckTypeEnum;
import com.hyjr.common.utils.LogUtils;
import com.hyjr.common.utils.PropertyUtil;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

//文件上传客户端
public class SendTaskClient {
	
	public void connect(int port, String host, final List<TaskBean> taskBeanList) throws Exception {
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
					.handler(new ChannelInitializer<Channel>() {
						
						@Override
						protected void initChannel(Channel ch) throws Exception {
							LogUtils.debug("采集端，客户端SendTaskClient，发送任务数据：" + taskBeanList.toString());
							ch.pipeline().addLast(new ObjectEncoder());
							ch.pipeline()
									.addLast(new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(null)));
							ch.pipeline().addLast(new SendTaskClientHandler(taskBeanList));
						}
					});
			ChannelFuture f = b.connect(host, port).sync();
			f.channel().closeFuture().sync();
			LogUtils.debug("采集端，客户端SendTaskClient，关闭");
		} finally {
			group.shutdownGracefully();
		}
	}

	public static void main(String[] args) {
		List<TaskBean> taskBeanList = new ArrayList<TaskBean>();
		
		String decrFileInfo = "root:$6$y9cP0qlmDYg$DvVGXa0RqJYJQqWOfAnlmQ95vAag1luXMzmbijf3nkH7R2diWrUTEyufJHyms227Vd6zvFXc3J4dIOdZRWCe10:16734:0:99999:7::::\n" + 
				"bin:*:15980:0:99999:7:::\n" + 
				"daemon:*:15980:0:99999:7:::";
		
		String encrFileInfo = null;
		try {
			encrFileInfo = SM2Utils.encrypt(PropertyUtil.getPropety("bcprov", "E2_pub_key"), decrFileInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
		
		for(int i = 0; i < 10; i++) {
			TaskBean taskBean = new TaskBean();
			taskBean.setHostIP("192.168.1." + (i + 1));//主机地址
			taskBean.setHostName("主机名称-" + (i + 1));//主机名称
			taskBean.setOsType("Linux");//操作系统类型
			taskBean.setOsVersion("3.2." + (i + 1));//操作系统版本
			taskBean.setCaptureDate(new Date());//采集时间
			taskBean.setCaptureSource("S6000");//采集宿主程序
			
			taskBean.setEncrFileInfo(encrFileInfo);//加密文件信息
			String checkType = CheckTypeEnum.SIMPLE.getKey();
			if(i % 2 == 1) {
				checkType += "," + CheckTypeEnum.MIDDLE.getKey();
			} else {
				checkType += "," + CheckTypeEnum.MIDDLE.getKey();
				checkType += "," + CheckTypeEnum.COMPLEX.getKey();
			}
			taskBean.setCheckTypes(checkType);//检测类型
			taskBeanList.add(taskBean);
		}
		
		try {
			new SendTaskClient().connect(Constants.SEND_PWD_PORT, "127.0.0.1", taskBeanList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
